home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / libs / mufs_usergroup.lha / usergroup / random.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  3KB  |  115 lines

  1. RCS_ID_C="$Id: random.c,v 1.1 1994/01/20 08:26:10 ppessi Exp $";
  2. /*
  3.  * random.c --- random numbers
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP User Library.
  8.  *
  9.  * Copyright ⌐ 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *
  12.  * Created      : Wed Sep 15 01:25:26 1993 ppessi
  13.  * Last modified: Wed Jan 19 10:21:44 1994 ppessi
  14.  */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/io.h>
  18. #include <devices/timer.h>
  19. #include <proto/timer.h>
  20.  
  21. extern struct Library *TimerBase;
  22.  
  23. /****i* random.module/LRandom ******************************************
  24. *
  25. *   NAME    
  26. *    LRandom -- Random number generator
  27. *
  28. *   SYNOPSIS
  29. *        value = LRandom()
  30. *
  31. *    LONG InitLRandom(void)
  32. *
  33. *   FUNCTION
  34. *      Generates a random long integer. The random number generator
  35. *      must have been initialized before calling LRandom() or strange
  36. *      things will happen.
  37. *
  38. *      LRandom() generates fairly good random numbers, all bits in the
  39. *      long word seem to be useful.
  40. *
  41. *   BUGS
  42. *      None known.
  43. *
  44. *   SEE ALSO
  45. *    InitLRandom, timer.device/GetSysTime()
  46. *
  47. ******************************************************************************
  48. */
  49.  
  50. static union random_seed {
  51.   struct timeval time;
  52.   ULONG          longs[2];
  53.   UWORD          words[4];
  54. } seed;
  55.  
  56. static const UWORD random_offset[4] = { 0x7823, 0xab34, 0x93b4, 0x7673 };
  57.  
  58. static const UWORD random_eor[4] = { 0xc97d, 0x6988, 0x32e9, 0x8487 };
  59.  
  60. ULONG LRandom(void)
  61. {
  62.   ULONG carry = 0;
  63.   int  i;
  64.  
  65.   for (i = 3; i >= 0; i--) {
  66.     carry += seed.words[i]*34639L + random_offset[i];
  67.     seed.words[i] = carry ^ random_eor[i];
  68.     carry >>= 16;
  69.   }
  70.  
  71.   return (ULONG)(seed.words[3] << 24) + (seed.words[2]<<16) + 
  72.     (seed.words[1] << 8) + seed.words[0];
  73. }
  74.  
  75. /****i* random.module/LRandomInit ******************************************
  76. *
  77. *   NAME    
  78. *    LRandomInit -- initialize LRandom generator
  79. *
  80. *   SYNOPSIS
  81. *        error = LRandomInit();
  82. *
  83. *    int LRandomInit(void);
  84. *
  85. *   FUNCTION
  86. *      Initializes the random number generator with the current time.
  87. *
  88. *   NOTES
  89. *      This function uses GetSysTime() to get the initial seed for the
  90. *      random number generator. Thus it needs the V36 timer.device.
  91. *
  92. *   RETURN VALUE
  93. *      Nonzero after initialization error.
  94. *
  95. *   BUGS
  96. *      None known.
  97. *
  98. *   SEE ALSO
  99. *    LRandom, timer.device/GetSysTime()
  100. *
  101. ******************************************************************************
  102. */
  103.  
  104. int LRandomInit(void)
  105. {
  106.   if (TimerBase) {
  107.     GetSysTime(&seed.time);
  108.     LRandom();   LRandom();   LRandom();   LRandom();
  109.     LRandom();   LRandom();   LRandom();   LRandom();
  110.     return 0;
  111.   }
  112.   return -1;
  113. }
  114.  
  115.